﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;

namespace Treasures
{
    public class LevelControl
    {

        #region fields and props

        #region events

        public event Action OnCrystalInfoChange;
        public event Action<Point, int> OnPointsInfoChange;

        #endregion

        private int _currentCrystalScore;

        private int _pointScore;

        public int Level { get; set; }

        public int PointDelta { get; private set; }

        public int InitialBias { get; private set; }

        public int PointScore 
        { 
            get
            {
                return _pointScore;
            }
            private set
            {
                _pointScore = value;
            }
        }


        public int CurrentCrystalScore 
        {
            get
            {
                return _currentCrystalScore;
            }
            set
            {
                _currentCrystalScore = value;
                OnCrystalInfoChange();
            }
        }

        public int Streak { get; set; }

        #endregion

        #region ctors
        public LevelControl()
        {
            this.InitialBias = 100;
            this.Level = 1;
            this.PointDelta = 50;
            this._pointScore = 0;
            this._currentCrystalScore = 0;
            this.Streak = 1;
        }

        public LevelControl(int pointDelta)
            : base()
        {
            this.PointDelta = pointDelta;
        }


        public void CountPoints(ChipFull[,] _map, List<IndexStruct> chips)
        {
            int count = chips.Count;
            int ret = 0;
            ret = this.InitialBias * this.Streak;
            
            if (count > 3 && count < 6)
            {
                for(int i = count; i > 3; i--)
                {
                    ret += (1 + i - 3) * this.PointDelta;
                }
            }
            if (count > 5)
            {
                int seq = count / 2;
                int mod = count % 2;
                if (seq > 3)
                {
                    for (int i = seq; i > 3; i--)
                    {
                        ret += (1 + i - 3) * this.PointDelta;
                    }
                }
                if (seq == 3)
                {
                    ret += 2 * this.InitialBias * this.Streak;
                }
                if (mod == 1)
                {
                    ret += 2 * this.PointDelta * this.Streak;
                }
                
            }

            for (int i = 0; i < count; i++)
            {
                if(_map[chips[i].i, chips[i].j].Chip.IsCrystaled)
                {
                    ret += this.PointDelta;
                }
            }
               
            //evaluating X, Y for textbox 
            //X
            int X = 0;
            int Y = 0;
            for (int i = 0; i < count; i++)
            {
                X += (int)ElementMap.Instance.PointMap[chips[i].i, chips[i].j].X;
                Y += (int)ElementMap.Instance.PointMap[chips[i].i, chips[i].j].Y;
            }
            X /= count;
            Y /= count;
            Y += Constants.initialElementWidth;
            this.PointScore += ret;
            OnPointsInfoChange(new Point(X, Y), ret);
            this.Streak++;
        }
 
        #endregion
    }
}
